package cn.hdu.liang.ch6;

import cn.hdu.liang.ch4.Dish;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collector;

import static cn.hdu.liang.ch4.Dish.menu;
import static java.util.stream.Collectors.*;

public class Partitioning {
    public static void main(String[] args) {
        Map<Boolean, List<Dish>> partMenu = menu.stream()
                .collect(partitioningBy(Dish::isVegetarian));
        System.out.println(partMenu);

        List<Dish> vegetDishes = menu.stream()
                .filter(Dish::isVegetarian)
                .collect(toList());
        System.out.println(vegetDishes);

        //筛选不是蔬菜的
        List<Dish> notVegetDishes = menu.stream()
                .filter(dish -> !dish.isVegetarian())
                .collect(toList());
        System.out.println(notVegetDishes);

        Collector<Dish, ?, Map<Dish.Type, List<Dish>>> partCollector = groupingBy(Dish::getType);
        Map<Boolean, Map<Dish.Type, List<Dish>>> vegetDishesByType = menu.stream()
                .collect(partitioningBy(Dish::isVegetarian, partCollector));
        System.out.println(vegetDishesByType);

        Collector<Dish, ?, Optional<Dish>> maxByCollector = maxBy(Comparator.comparingInt(Dish::getCalories));
        Collector<Dish, ?, Dish> dishCollector = collectingAndThen(maxByCollector, Optional::get);
        Map<Boolean, Dish> mostCaPartByVeget = menu.stream()
                .collect(partitioningBy(Dish::isVegetarian, dishCollector));
        System.out.println(mostCaPartByVeget);
    }
}
